﻿using System;
using System.Net;
using System.Threading;
using WeatherAggregator.WeatherSources;

namespace WeatherAggregator.ConsoleUi
{
    class Program
    {
        static void Main(string[] args)
        {
			if (WebRequest.DefaultWebProxy != null)
				WebRequest.DefaultWebProxy.Credentials = new MyCredentials("credentials.txt");


            var a = new Aggregator(Properties.Settings.Default.ConnectionString);

            a.Aggregation += a_Aggregation;
			a.Error += a_Error;

			a.AddSource(new MeteocenterSource { IcaoCode = "USSS", PointName = "Екатеринбург" });
			a.AddSource(new MeteocenterSource { IcaoCode = "UUDD", PointName = "Москва" });
			a.AddSource(new MeteocenterSource { IcaoCode = "USPP", PointName = "Пермь" });

			a.AddSource(new WeatherComRssSource { CityCode = "RSXX1219" , PointName = "Екатеринбург"});
			a.AddSource(new WeatherComRssSource { CityCode = "RSXX0063", PointName = "Москва" });
			a.AddSource(new WeatherComRssSource { CityCode = "RSXX0082", PointName = "Пермь" });


            a.Start();


            Console.WriteLine("Gathering... Press any key to end.");
            Console.ReadKey(true);

        }

		static void a_Error(object sender, ThreadExceptionEventArgs e)
		{
			Console.Error.WriteLine("Error during aggregation.{0}{1}", Environment.NewLine, e.Exception);
		}

        static void a_Aggregation(object sender, AggregationInfoEventArgs e)
        {
        	Console.WriteLine("Gethered. {0} entries added (from {3} src). Next at {1:T} (+{2:g})",
        	                  e.GatheredEntriesCount,
        	                  e.NextExpectedFire.ToLocalTime(),
        	                  e.NextExpectedFire - DateTime.UtcNow,
        	                  e.CheckedSourcesCount
        		);
        }
    }
}
